//
//  PopoverPresentationController.swift
//  WeiBo
//
//  Created by Tony on 2018/1/2.
//  Copyright © 2018年 Tony. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController {
    
    /*
     每个Model出来的控制器，其内部都有一个containerView，
     所有view都是containerView的subview被Model出来
     */
    
    // MARK: -对外提供属性
    var presentedViewFrame: CGRect = CGRect.zero
    
    /// 布局containerView内subview
    override func containerViewDidLayoutSubviews() {
        // 一般不在这里设置subviewframe
    }
    
    /// 即将布局containerView内subview
    override func containerViewWillLayoutSubviews() {
        
        // 设置被弹出控制器的frame     
        presentedView?.frame = presentedViewFrame
        
        // 添加蒙版
        addConver()
        
    }
}
extension PopoverPresentationController {
     func addConver() {
    
        // 初始化
        let conver: UIView = UIView()
        conver.backgroundColor = UIColor.init(white: 0.5, alpha: 0.2)
        conver.frame = containerView!.bounds
        
        // 添加手势
        let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(PopoverPresentationController.converClick))
        conver.addGestureRecognizer(tapGesture)
        
        // 添加蒙版
        containerView?.insertSubview(conver, belowSubview: presentedView!)
    }
    @objc private func converClick() {

        presentedViewController.dismiss(animated: true, completion: nil)
    }
    
}
